<template>
	<view class="apps">
		<view class="flex fdc plr20" v-if="list.length >0">
			<view class="flex plr10 ptb10 mb20 pr" :style="style[index%4]" v-for="(item,index) in list" :key="index">
				<view class="f1 flex fdc ptb10 plr10 ">
					<view class="flex aic">
						<uv-avatar :src="logo"></uv-avatar>
						<view class="f26 white ml20">{{item.title}}</view>
					</view>
					
					<view class="flex mtb20 jcsa">
						<view class="white flex aic f22 ptb10" v-if="item.money > 0">{{parseInt(item.money)}}元水果福利</view>
						<view class="white flex aic f22 ptb10" v-if="item.score > 0">{{parseInt(item.score)}}电瓶积分</view>
						<view class="white flex aic f22 ptb10" v-if="item.is_vip == 1">300元洗车年费</view>
						<view class="white flex aic f22 ptb10" v-if="item.is_vip == 0">300元洗车充值卡，每年押金30元</view>
					</view>
					<view class="flex white aic">
						<view class="f18">￥</view><view class="ft30">{{item.price}}</view><view class="ml10 f26" v-if="item.gift">赠送 ￥{{item.gift}} 优惠券</view>
					</view>
				</view>
				<view class="pro_right"><image :src="image" ></image></view>
				<view class="btns flex aic" @click="handle(item.id)">详情 <uv-icon name="level" color="#fff" size="20"></uv-icon> </view>
			</view>
		</view>
	</view>
</template>

<script>
	import { util } from '/common/util.js';
	let app;
	export default {
		data() {
			return {
				list:[],
				logo:"https://mp.lifecloud.top/static/images/logo.jpg",
				image:"https://mp.lifecloud.top/static/images/logo.png",
				style:[
					"background-color:#af0203;border-radius: 4px;",
					"background-color:#7bbf31;border-radius: 4px;",
					"background-color:#3a5fc7;border-radius: 4px;",
					"background-color:#e0be5e;border-radius: 4px;",
					
				]
			}
		},
		onLoad() {
			app = this;
			this.getList();
		},

		methods: {
			getList(){
				util.request({
					url:"/shop/index",
					method:"POST",
				}).then((res)=>{
					if(res.code ==1){
						app.list = res.data;
					}
				})
			},
			handle(id){
				if(util.isLogin == false){
					util.alert('请先登录');
					setTimeout(function(){
						uni.navigateTo({
							url:"/pages/login/login"
						})
					},1500)
					return
				}
				uni.navigateTo({
					url:"/pages/shop/product?id="+id,
				})
			},
			async buy(item){
				if(util.isLogin == false){
					util.alert('请先登录');
					setTimeout(function(){
						uni.navigateTo({
							url:"/pages/login/login"
						})
					},1500)
					return
				}
				const order = await util.request({url:'/shop/cart',data:item});
				if(order.code == 1){
					const payment = await util.request({url:'/shop/payment',data:order.data});
					if(payment.code == 1){
						wx.requestPayment({
							timeStamp: payconfig.data.payment.timeStamp,
							nonceStr: payconfig.data.payment.nonceStr,
							package: payconfig.data.payment.package,
							signType: payconfig.data.payment.signType,
							paySign: payconfig.data.payment.paySign,
							success (res) {
								util.request({url:'/shop/prepaid',data:order});
								util.alert('支付成功');
								setTimeout(function(){
									uni.redirectTo({
										url:"/pages/order/order?id="+payconfig.data.order.id
									})
								},1500)
							},
							fail (err) {
								util.request({url:'/shop/payfail',data:order});
								if(err.errMsg === 'requestPayment:fail cancel'){
									util.alert('您取消了支付');
								}else{
									util.alert('支付失败');
								}
							},
							comlete(res){
								console.log(res);
							}
						})
					}
				}
			}
		}
	}
</script>

<style scoped>
.pro_right{width:240rpx;height:240rpx;}
.pro_right image{width:100%;height:100%}
.btns{position: absolute; top:20rpx; right:20rpx; font-weight: 400; font-size: 26rpx; padding:10rpx 15rpx; border-radius: 8rpx; border:1px dashed  #fff;color:#fff; }
</style>
